﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[AddComponentMenu("Tween/Tween Scale Axis")]
public class TweenScaleAxis : UITweener
{
    public enum Axis
    {
        Axis_X = 0,
        Axis_Y = 1,
        Axis_Z = 2,

    }

    public Axis axis = Axis.Axis_X;


    public float from;
    public float to;

    //public Vector3 from = Vector3.one;
    //public Vector3 to = Vector3.one;

    Transform mTrans;

    public Transform cachedTransform { get { if (mTrans == null) mTrans = transform; return mTrans; } }

    //public Vector3 value { get { return cachedTransform.localScale; } set { cachedTransform.localScale = value; } }

    public float value
    {
        get
        {
            switch (axis)
            {
                case Axis.Axis_X:
                    return cachedTransform.localScale.x;
                case Axis.Axis_Y:
                    return cachedTransform.localScale.y;
                case Axis.Axis_Z:
                    return cachedTransform.localScale.z;
            }
            return cachedTransform.localPosition.x;
        }
        set
        {
            switch (axis)
            {
                case Axis.Axis_X:
                    cachedTransform.localScale = new Vector3(value, cachedTransform.localScale.y, cachedTransform.localScale.z);
                    break;
                case Axis.Axis_Y:
                    cachedTransform.localScale = new Vector3(cachedTransform.localScale.x, value, cachedTransform.localScale.z);
                    break;
                case Axis.Axis_Z:
                    cachedTransform.localScale = new Vector3(cachedTransform.localScale.x, cachedTransform.localScale.y, value);
                    break;
            }
        }
    }



    /// <summary>
    /// Tween the value.
    /// </summary>

    protected override void OnUpdate(float factor, bool isFinished)
    {
        value = from * (1f - factor) + to * factor;
    }

    /// <summary>
    /// Start the tweening operation.
    /// </summary>

    static public TweenScale Begin(GameObject go, float duration, Vector3 scale)
    {
        TweenScale comp = UITweener.Begin<TweenScale>(go, duration);
        comp.from = comp.value;
        comp.to = scale;

        if (duration <= 0f)
        {
            comp.Sample(1f, true);
            comp.enabled = false;
        }
        return comp;
    }

    [ContextMenu("Set 'From' to current value")]
    public override void SetStartToCurrentValue() { from = value; }

    [ContextMenu("Set 'To' to current value")]
    public override void SetEndToCurrentValue() { to = value; }

    [ContextMenu("Assume value of 'From'")]
    void SetCurrentValueToStart() { value = from; }

    [ContextMenu("Assume value of 'To'")]
    void SetCurrentValueToEnd() { value = to; }
}
